package com.qijian.common.utils.sync;


import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.qijian.common.utils.StringUtils;
import lombok.SneakyThrows;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class GetKingDeeData extends BaseKingDee {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");

    public static void main(String[] args) {

        GetKingDeeData kingDee = new GetKingDeeData();
//        cn.hutool.json.JSONArray array = getProject(null, "GZ21496");
//        System.out.println(array);
//        List<Map<String, Object>> data = kingDee.getSalesContract();
//        List<JSONObject> data = kingDee.getAccountPayablbGet(null);
//        List<Map> data = kingDee.getItemClassify();
//        List<Map> data1 = kingDee.getItemClassify1();
//        List<Map> data = kingDee.getAttribute();
//        List<Map> data = kingDee.getAttributeValue();
//        List<Map> data = kingDee.getDict("5e589cb876ebb4");
//        List<Map> data = kingDee.getCustomer();
//        List<JSONObject> data = kingDee.getOutbound();
//        List<JSONObject> data = kingDee.getAccountReceived(null);
//        List<JSONObject> data = kingDee.getSubjectType(null);
//        List<JSONObject> data = kingDee.getSubject(null);
//        List<JSONObject> data = kingDee.getVoucher(Lists.newArrayList("6603","6602", "1403.01"
//                , "5001.01", "5001.02", "5001.03", "5001.04", "5001.05", "6601"));
//        List<JSONObject> data = kingDee.getReportByType();
//        List<JSONObject> data = kingDee.getAccountPayable("FDate > '2022-07-13 00:00:00.000'");
//        List<JSONObject> data = kingDee.getAccountAmount(null);
//        List<Map> data = kingDee.getPurchaseOrder();
//        List<JSONObject> data = kingDee.getProject("F_ora_Text2 = 'S074'");
        List<JSONObject> data = kingDee.getSupplier("FModifyDate > '2022-07-13 00:00:00.000'");
//        List<JSONObject> data = kingDee.getItemGet(null);
//        List<JSONObject> data = kingDee.getItemByIds(Collections.singleton("1885805"));
//        List<JSONObject> data = kingDee.getStock();
//        List<JSONObject> data = kingDee.getUnit();
//        List<JSONObject> data = kingDee.getPurchaseOrder(null);
//        List<JSONObject> data = kingDee.getPaidApply(null);
//        List<JSONObject> data = kingDee.getPurchaseRequisition(null);
//        List<JSONObject> data = kingDee.kingdeeUserIdTransition();
//        List<JSONObject> data = kingDee.getVoucher("FYEAR = '2022' and FPERIOD = '4'",Collections.singleton("6601.05"));
//        List<JSONObject> data = kingDee.getSTKInventory(null);


//        System.out.println(data.get(0));
        System.out.println(data);
        System.out.println(data.size());
    }


    public List<Map<String, Object>> getSalesContract() {
        return getSalesContract(null);
    }

    /**
     * 获取销售订单
     */
    public List<Map<String, Object>> getSalesContract(String conditions) {
        String formId = "SAL_SaleOrder"; //
        String fieldKeys = "FID,FBillNo" +
                ",FCustId,FCustId.FName,F_BEQ_Date,FBillTypeID.FName,F_BEQ_Text1,F_BEQ_Text" +
                ",FDocumentStatus,FApproverId.FName,FApproveDate" +
                ",F_ora_Base,F_ora_Base.FName,FCloseStatus,FMrpCloseStatus,FCancelStatus" +
                ",FSalerId,FSalerId.FName" +
                ",FCreatorId.FUserAccount,FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ",FSaleOrderEntry_FEntryID" +
                ",FMaterialId,FMaterialName,FMaterialId.FNumber" +
                ",FMaterialModel,FUnitID.FName,FQty" +
                ",F_ora_MulBase,F_ora_MulBase.F_ORA_TEXT" +
                ",FEntryTaxRate,FEntryTaxAmount,FTaxAmount_LC,FAmount,FAmount_LC,FAllAmount,FAllAmount_LC" +
                ",FDeliveryDate,FTaxPrice,FPrice" +
                ",FSettleCurrId.FName,FExchangeRate" +
                ",FCANOUTQTY,FDeliveryStatus,FStockOutQty" +
                "" +
                ",FSaleOrgId,FSaleOrgId.FName" +
                "" +
                ",FOrderEntryPlan_FDetailID,FPlanUnitId.FName,FPlanQty,F_ora_DZ" +
                ",F_ora_Date2,F_ora_Date3,F_ora_Date6,F_ora_Date8,F_ora_Integer,F_BEQ_Date1,F_BEQ_Base2.FName,F_BEQ_Base3.FName,Fbeizhu,F_BEQ_Amount3";
        //默认排除振江两家自己的公司，连云港轨道交通&开特&
        String filterString = "(FSaleOrgId = '100121' or FSaleOrgId = '100122') " +
                " and FCustID != 615655 and FCustID != 1438297" +
                " and FDocumentStatus = 'C'";
        if (conditions != null)
            filterString += StringUtils.startsWith(conditions.toLowerCase().trim(), "and") ? conditions : " and " + conditions;
        List<Map<String, Object>> data = this.getData(formId, fieldKeys, filterString);
        return data;
    }


    /**
     * 来料通知单
     * @param conditions
     * @return
     */
    public List<Map<String, Object>> getReceiveBill(String conditions) {
        String formId = "PUR_ReceiveBill"; //
        String fieldKeys = "FID,FBillNo,FBillTypeID.FName,FDate" +
                ",FDocumentStatus,FApproverId.FName,FApproveDate,FSupplierId,FSupplierId.FNAME,FCancelStatus,FMaterialId.FNumber" +
                ",FCreatorId.FUserAccount,FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ",FEntryTaxRate,FEntryTaxAmount,FTaxAmount_LC,FAmount,FAmount_LC,FAllAmount,FAllAmount_LC" +
                ",FDetailEntity_FEntryID,FMaterialId,FMaterialName,FMateriaModel,FMateriaType,FAuxPropId,FProduceDate,FExpiryDate,FPriceUnitId.FName" +
                ",FAUXPROPID.FF100001.fdatavalue" +
                ",FAUXPROPID.FF100002.fdatavalue" +
                ",FAUXPROPID.FF100003" +
                ",FAUXPROPID.FF100004" +
                ",FAUXPROPID.FF100005.fdatavalue" +
                ",FAUXPROPID.FF100006.fdatavalue" +
                ",FAUXPROPID.FF100007" +
                ",FAUXPROPID.FF100008" +
                ",FAUXPROPID.FF100009" +
                ",FAUXPROPID.FF100011" +
                ",FAUXPROPID.FF100013" +
                ",FAUXPROPID.FF100015.fdatavalue" +
                ",FAUXPROPID.FF100016.fdatavalue" +
                ",FAUXPROPID.FF100017.fdatavalue" +
                ",FAUXPROPID.FF100018.fdatavalue" +
                ",FAUXPROPID.FF100019.fdatavalue" +
                ",FAUXPROPID.FF100020.fdatavalue" +
                ",FAUXPROPID.FF100021" +
                ",FAUXPROPID.FF100022" +
                ",FAUXPROPID.FF100023" +
                ",FPrice,FMustQty,FStockUnitID,FStockID.FName,FStockQty,FStockGroupId.FName";
        //默认排除振江两家自己的公司，连云港轨道交通&开特&
        String filterString = "FStockOrgId = 100121" +
                " and FDocumentStatus = 'C'";
        if (conditions != null)
            filterString += StringUtils.startsWith(conditions.toLowerCase().trim(), "and") ? conditions : " and " + conditions;
        List<Map<String, Object>> data = this.getData(formId, fieldKeys, filterString);
        return data;
    }

    /**
     * 物流发运单
     *
     * @return
     */

    public List<JSONObject> getOutbound() {
        return getOutbound(null);
    }

    public List<JSONObject> getOutbound(String conditions) {
        String formId = "STK_TransferDirect"; //
        String fieldKeys = "FID,FBillNo" +
                ",FCustID,FCustID.FName,FDate" +
                ",FSettleModeId.FName,F_ora_Base4.FName,F_ora_Base1.FName,F_ora_BGDH,F_ora_BGFPH,F_ora_Base2.FName" +
                ",F_ora_Decimal2,F_ora_Decimal,F_ora_Decimal1,FNote" +
                ",FCreatorId,FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ",FApproverId.FName,FApproveDate,FDocumentStatus,FCancelStatus,FExchangeRate,FSETTLECURRID" +
                "" +
                ",FBillEntry_FEntryID,FSOEntryId,F_ora_Text1" +
                ",FMaterialId,FMaterialId.FName,FMaterialId.FNumber" +
                ",FBaseUnitId.FName,FSecUnitId.FName,FQty,FExtAuxUnitQty" +
                ",F_ora_Decimal6,F_ora_Decimal5,F_ora_Decimal4,FNoteEntry" +
                ",FConsignPrice,FTaxPrice,FTaxRate,FConsignAmount,FTaxAmount,FAllAmount" +
                ",FBillEntry_Link_FSBillId,FBillEntry_Link_FSTableName,FLot.FName,F_ora_Qty1" +
                "" +
                "" +
                ",F_ora_Base5,F_ora_Base5.FName,F_ora_Base3.FName,F_ora_Base6,F_ora_Base6.FName,FBillStatus,FBillStatus1,F_ora_Amount,F_ora_Amount1,F_ORA_HL" +
                "";
        String filterString = "FStockOutOrgId = '100121'" +
                "";
        if (conditions != null)
            filterString += StringUtils.startsWith(conditions.toLowerCase().trim(), "and") ? conditions : " and " + conditions;
        return getDataJson(formId, fieldKeys, filterString);
    }


    /**
     * 客户
     *
     * @return
     */
    public List<Map<String, Object>> getCustomer() {
        return getCustomer(null);
    }

    public List<Map<String, Object>> getCustomer(String condition) {
        String formId = "BD_Customer"; //
        String fieldKeys = "FCUSTID" +
                ",FName" +
                ",FNumber" +
                ",FDocumentStatus,FForbidStatus" +
                ",FCreateOrgId,FUseOrgId" +
                ",FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                "";
//        String filterString = "(FUseOrgId = '100121' or FUseOrgId = '100122')";
//        String filterString = "FUseOrgId = '100121' and FForbidStatus = 'A'";
        String filterString = "FForbidStatus = 'A'";
        filterString = filterString + (StringUtils.isNotBlank(condition) ? " and " + condition : "");
        return this.getData(formId, fieldKeys, filterString);
    }

    /**
     * 供应商
     *
     * @return
     */
    public List<JSONObject> getSupplier(String condition) {
        String formId = "BD_Supplier";
        String fieldKeys = "FSupplierId" +
                ",FName" +
                ",FNumber" +
                ",FSupplierClassify" +
                ",FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate,FUseOrgId.FNumber" +
                ",FContact,FPost,FTel,FMobile,FEMail,FCommonContactId,FContactNumber,FGender,FSupplierContact_FContactId";
//        String filterString = "(FUseOrgId = '100121' or FUseOrgId = '100122' " +
//                "or FUseOrgId = '100124' or FUseOrgId = '878319') " + (condition == null ? "" : " and " + condition) +
//                "";
        String filterString = "(FUseOrgId = '100121' ) " + (condition == null ? "" : " and " + condition) +
                "";

//        List<Map<String, Object>> data = this.getData(formId, fieldKeys, filterString);
        return getDataJson(formId, fieldKeys, filterString);
    }

    /**
     * 物料分类
     */
    public List<Map<String, Object>> getItemClassify() {
        String formId = "CPCM_MATERIALGROUP";
        String fieldKeys = "FID" +
                ",FName" +
                ",FNumber" +
                ",FFullParentId" +
                ",FParentId" +
                ",FLeft,FRight,FGroupId" +
                "";
        String filterString = "FGroupId like '%-%'";
        List<Map<String, Object>> data = this.getData(formId, fieldKeys, filterString);
        return data;
    }


    public List<Map<String, Object>> getItemClassify1() {
        String formId = "SAL_MATERIALGROUP";
        String fieldKeys = "FID" +
                ",FName" +
                ",FNumber" +
                ",FParentId" +
                "" +
                "";
        String filterString = "";
        List<Map<String, Object>> data = this.getData(formId, fieldKeys, filterString);
        return data;
    }

    /**
     * 开工令
     *
     * @return
     */
    public List<Map<String, Object>> getProjectNo() {
        String formId = "k441ff31b955046bab1f7c2b819d60501"; //

        // 项目号，项目名称，机台号
        String fieldKeys = "F_ora_Text2,F_ora_Text1,F_ora_Text3,FCreateDate,FBILLSTATUS,F_BEQ_BASE2" +
                ",FCreatorId.FName" +
                ",FModifierId.FName,FModifyDate,FID";
        String filterString = "FCreateDate > '2021-12-30'";
        List<Map<String, Object>> result = this.getData(formId, fieldKeys, filterString);
        return result;
    }


    /**
     * 辅助属性值
     *
     * @return
     */
    public List<Map<String, Object>> getAttributeValue() {
        String formId = "BD_AuxPtyValue"; //

        // 项目号，项目名称，机台号
        String fieldKeys = "FAuxPtyValueID,FBillNo" +
                ",FMaterialAuxPropertyId,FMaterialAuxPropertyId.FName" +
                ",FMaterialId,FMaterialId.FName,FMaterialId.FNumber" +
                ",FForbidStatus,FIsSysPreInsert" +

                ",FEntity_FEntryID,FAuxPtyId,FAuxPtyName,FAuxPtyNumber,FIsSelect,FIsDisable,FDisplaySeq,FIsDefault" +


                ",FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate";
        String filterString = "";
        List<Map<String, Object>> result = this.getData(formId, fieldKeys, filterString);
        return result;
    }

    public List<Map<String, Object>> getAttribute() {
        String formId = "BD_FLEXAUXPROPERTY"; //

        // 项目号，项目名称，机台号
        String fieldKeys = "FID,FNAME,FNUMBER" +
                ",FVALUETYPE" +
                ",FFORBIDSTATUS" +
                ",FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate";
        String filterString = "";
        List<Map<String, Object>> result = this.getData(formId, fieldKeys, filterString);
        return result;
//                result.stream().collect(
//                Collectors.collectingAndThen(Collectors.toCollection(
//                        () -> new TreeSet<>(Comparator.comparing(m -> String.valueOf(m.get("FMaterialAuxPropertyId"))))), ArrayList::new));
    }

    /**
     * 辅助资料
     *
     * @return
     */
    public List<Map<String, Object>> getDict(String fid) {
        String formId = "BOS_ASSISTANTDATA_DETAIL"; //

        // 项目号，项目名称，机台号
        String fieldKeys = "FEntryID,FNumber,FDataValue,FId,FId.FNumber,FId.Fname,FSeq,FForbidStatus,FIsSysPreset" +
                ",FUseOrgId,FCreateOrgId" +
                ",FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate";
        String filterString = "FId.Fname in (" + fid + ")";
        List<Map<String, Object>> result = this.getData(formId, fieldKeys, filterString);
        return result;
    }

    /**
     * 物料绑定辅助属性
     *
     * @return
     */
    public List<Map<String, Object>> getItemAttr() {
        String formId = "BD_MATERIAL"; //
        String fieldKeys = "FMATERIALID,FName,FNumber" +
                ",FEntityAuxPty_FEntryID,FIsEnable1,FIsMustInput" +
                ",FAuxPropertyId,FAuxPropertyId.FName" +
                ",FValueType,FValueSet" +
                ",FUseOrgId11" +
                ",FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate";
//        String filterString = "FNumber = 'BC.GB.001'";
        String filterString = "FUseOrgId = 100121 and FIsEnable1 = 1";
        List<Map<String, Object>> result = this.getData(formId, fieldKeys, filterString);
        return result;
    }

    /**
     * 采购订单
     *
     * @return
     */
    public List<JSONObject> getPurchaseOrder(String condition) {
        String formId = "PUR_PurchaseOrder"; //
        String fieldKeys = "FID,FBillNo,FDate" +
                ",FBillTypeID.FName,FBusinessType,FSourceBillNo" +
                ",FCancelStatus,FCloseStatus,FCloserId.FName" +
                ",FSupplierId,FSupplierId.FName,FDocumentStatus,FConfirmStatus" +
                ",FPurchaserId,FPurchaserId.FName,FApproverId,FApproverId.FName,FApproveDate" +
                ",FBillAmount,FBillTaxAmount,FBillAllAmount,FBillAmount_LC,FBillTaxAmount_LC,FBillAllAmount_LC" +
                ",FSettleCurrId.FName,FExchangeRate" +
                ",FCreatorId,FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate,F_ora_LargeText,F_ora_Combo" +
                "" +
                ",F_ora_Text3,FProviderContactId,FProviderContactId.FName,FSettleModeId.FName,FEntrySettleModeId" +
                ",F_ora_UnitID.FName" +
                ",FPOOrderEntry_FEntryID,FMaterialId,FMaterialId.FName,FMaterialId.FNumber" +
                ",FUnitId.FName,FQty,FStockInQty,FMrbQty,FReceiveQty,F_ora_Qty" +
                ",FEntryAmount,FEntryTaxAmount,FAllAmount,FAmount_LC,FTaxAmount_LC,FAllAmount_LC" +
                ",FPrice,FTaxPrice,FEntryTaxRate,FEntryDiscountRate" +
                ",FDeliveryDate,FSrcBillTypeId,FPOOrderEntry_Link_FSBillId,FPOOrderEntry_Link_FSId,FSrcBillNo,FEntryNote" +
                ",FPOOrderEntry_Link_FSBillId,FDeliveryEarlyDate,FDeliveryLastDate" +
                "" +
                ",FEntryDeliveryPlan_FDetailId,FDeliCommitQty,FDeliRemainQty,FPlanQty,FELocationAddress" +
                ",FPlanUnitID.FName,FDeliveryDate_Plan" +
                ",FSUPPLIERDELIVERYDATE,FTRLT,FPREARRIVALDATE,FConfirmDeliQty,FConfirmDeliDate,FConfirmInfo" +
                "" +
                ",FAUXPROPID.FF100001.fdatavalue" +
                ",FAUXPROPID.FF100002.fdatavalue" +
                ",FAUXPROPID.FF100003" +
                ",FAUXPROPID.FF100004" +
                ",FAUXPROPID.FF100005.fdatavalue" +
                ",FAUXPROPID.FF100006.fdatavalue" +
                ",FAUXPROPID.FF100007" +
                ",FAUXPROPID.FF100008" +
                ",FAUXPROPID.FF100009" +
                ",FAUXPROPID.FF100011" +
                ",FAUXPROPID.FF100013" +
                ",FAUXPROPID.FF100015.fdatavalue" +
                ",FAUXPROPID.FF100016.fdatavalue" +
                ",FAUXPROPID.FF100017.fdatavalue" +
                ",FAUXPROPID.FF100018.fdatavalue" +
                ",FAUXPROPID.FF100019.fdatavalue" +
                ",FAUXPROPID.FF100020.fdatavalue" +
                ",FAUXPROPID.FF100021" +
                ",FAUXPROPID.FF100022" +
                ",FAUXPROPID.FF100023" +
                "";
//        condition = "FBillNo = 'CGDD-10001-202402-0257'";
        String filterString = "FPurchaseOrgId = 100121" + (condition == null ? "" : " and " + condition) +
                "";
        return this.getDataJson(formId, fieldKeys, filterString);
    }

    /**
     * 简单生产领料
     *
     * @return
     */
    public List<JSONObject> getItemGet(String condition, Integer sz) {
        buffered_sz = sz;
        List<JSONObject> result = getItemGet(condition);
        buffered_sz = buffered_sz_default;
        return result;
    }

    @SneakyThrows
    public List<JSONObject> getItemGet(String condition) {
//        Date endDate = DateUtils.addMonths(startDate, 1);

        String formId = "SP_PickMtrl"; //
        String fieldKeys = "" +
                "FID,FBillNo,FDocumentStatus,FBillType.FName,FDate,FDescription" +
                ",FStockerId,FStockerId.FName,FWorkShopId,FWorkShopId.FName" +
                ",F_ora_Base1,F_ora_Base1.FName" +
                ",F_ora_Text" +
                ",FSrcBillNo,FEntity_Link_FSBillId,FEntity_Link_FSId,FEntity_Link_FSTableName" +
                ",FCancelStatus,FCreatorId,FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ",FApproverId,FApproverId.FName,FApproveDate" +
                "" +
                ",FEntity_FEntryID" +
                ",FMaterialId,FMaterialId.FName,FMaterialId.FNumber,FSpecification" +
                ",FUnitID.FName,FSecUnitId.FName" +

                ",FAUXPROPID.FF100001.fdatavalue" +
                ",FAUXPROPID.FF100002.fdatavalue" +
                ",FAUXPROPID.FF100003" +
                ",FAUXPROPID.FF100004" +
                ",FAUXPROPID.FF100005.fdatavalue" +
                ",FAUXPROPID.FF100006.fdatavalue" +
                ",FAUXPROPID.FF100007" +
                ",FAUXPROPID.FF100008" +
                ",FAUXPROPID.FF100009" +
                ",FAUXPROPID.FF100011.FDATAVALUE" +
                ",FAUXPROPID.FF100013" +
                ",FAUXPROPID.FF100015.fdatavalue" +
                ",FAUXPROPID.FF100016.fdatavalue" +
                ",FAUXPROPID.FF100017.fdatavalue" +
                ",FAUXPROPID.FF100018.fdatavalue" +
                ",FAUXPROPID.FF100018.fnumber" +
                ",FAUXPROPID.FF100019.fdatavalue" +
                ",FAUXPROPID.FF100020.fdatavalue" +
                ",FAUXPROPID.FF100021" +
                ",FAUXPROPID.FF100022" +
                ",FAUXPROPID.FF100023" +
                "" +
                ",FAppQty,FActualQty,FExtAuxUnitQty,FSecActualQty" +
                ",FLot.FName" +
                ",FEntrtyMemo,F_ORA_BASE.F_ORA_TEXT" +
                ",FPrice,FAmount" +
                "" +
                ",FEntrySrcInterId,FEntrySrcEnteryId,FSrcBillType" +
                "" +
                ",FStockId,FStockId.FName,FStockLocId" +
                ",F_ora_Qty" +
                "";
        String filterString = "FStockOrgId = 100121" +
//                " and FDate>='" + DateUtil.format(startDate, "yyyy-MM-dd") + "' and FDate < '" + DateUtil.format(endDate, "yyyy-MM-dd") + "'" +
                (StringUtils.isNotBlank(condition) ? AND + condition : "") +
                "";

        List<JSONObject> dataJson = this.getDataJson(formId, fieldKeys, filterString);
        buffered_sz = buffered_sz_default;
        return dataJson;
    }

    public List<JSONObject> getAccountPaidGet(String updateTime, Integer sz) {
        buffered_sz = sz;
        List<JSONObject> result = getAccountPaidGet(updateTime);
        return result;
    }

    /**
     * 付款单
     *
     * @param updateTime
     * @return
     */
    public List<JSONObject> getAccountPaidGet(String updateTime) {
        String formId = "AP_PAYBILL"; //
        String fieldKeys = "" +
                "FID,FBillNo,FDOCUMENTSTATUS,FCreatorId,FAPPROVERID,FCreateDate,FSETTLEORGID,FApproveDate," +
                "FPURCHASEORGID,FPAYTOTALAMOUNTFOR_H,FCURRENCYID,FDATE,FModifyDate,FModifierId,FWRITTENOFFSTATUS," +
                "FBillTypeID,FBillTypeID.FNAME,FPURCHASERID,FPURCHASERID.FNAME,FREMARK,FPURCHASERGROUPID,FPURCHASEDEPTID,FREALPAYAMOUNTFOR_H,FACCOUNTSYSTEM," +
                "FPAYAMOUNT,FCONTACTUNITTYPE,FCONTACTUNIT,FRECTUNITTYPE,FRECTUNIT,FRECTUNIT.FNAME,FDepartment,FPAYORGID, FPAYORGID.FNAME,FPAYAMOUNTFOR," +
                "FEXCHANGETYPE,FMAINBOOKID,FSETTLECUR,FPAYTOTALAMOUNT_H,FPAYAMOUNT,FREALPAYAMOUNT_H,FEXCHANGERATE,FBookingDate," +
                "FCreatorId,FCreateDate,FModifierId,FModifyDate,FCreatorId.FNAME,FModifierId.FNAME,FCancelStatus," +
                "" +
                "FPAYBILLENTRY_FEntryID,FSETTLETYPEID,FSETTLEPAYAMOUNTFOR,FSETTLEDISTAMOUNTFOR,FPAYTOTALAMOUNTFOR,FOPPOSITEBANKACCOUNT," +
                "FCOMMENT,FOPPOSITECCOUNTNAME,FPAYITEM,FPURPOSEID, FPURPOSEID.FNAME,FPAYAMOUNTFOR_E,FPayType,FQTY," +
                "" +
                "FPAYBILLSRCENTRY_FEntryID,FSRCBILLID,FSRCROWID,FSETTLETYPEID,FSOURCETYPE,FSRCBILLNO,FSRCCURRENCYID,FSRCCURRENCYID.FNAME,FEXPIRY,FAFTTAXTOTALAMOUNT,FREALPAYAMOUNT_S," +
                "FSRCSETTLETYPEID.FNAME,FPAYPURPOSEID,FPAYPURPOSEID.FNAME,FSRCCOSTID, FSRCCOSTID.FNAME,FPAYAMOUNT_E,FPAYTOTALAMOUNT," +
                "" +
                "FOPPOSITEBANKACCOUNT,FOPPOSITECCOUNTNAME,FOPPOSITEBANKNAME,FCURRENCYID.FNAME,FSETTLETYPEID.FNAME," +
                "FPAYBILLSRCENTRY_Link_FSBillId, FPAYBILLSRCENTRY_Link_FSId";
        String filterString = "FSETTLEORGID = 100121";
        if (updateTime != null)
            filterString += StringUtils.startsWith(updateTime.toLowerCase().trim(), "and") ? updateTime : " and " + updateTime;
        return this.getDataJson(formId, fieldKeys, filterString);
    }


    /**
     * 获取应收款
     *
     * @param updateTime
     * @return
     */
    public List<JSONObject> getAccountReGet(String updateTime) {

        String formId = "AR_receivable";
        String fieldKeys = "" +
                "FID,FBillNo,FDocumentStatus,FCUSTOMERID,FCUSTOMERID.FNAME,FCURRENCYID.FNAME,FDATE,FSETTLEORGID," +
                "FENDDATE_H,FPayConditon,FPayConditon.FNAME,FSALEDEPTID,FSALEDEPTID.FNAME,FSALEERID,FSALEERID.FNAME,FAR_Remark,FCreatorId,FCreatorId.FNAME," +
                "FCreateDate,FModifyDate,FModifierId,FModifierId.FNAME,FORDERENTRYID,FOPENSTATUS,FCancelStatus," +
                "FPAYORGID,FPAYORGID.FName," +
                "FSOURCEBILLTYPEID.FName,FSRCROWID," +
                "" +
                "FEntityDetail_FEntryID,FSRCROWID,FSourceBillNo,FLot,FLot.FNAME," +
                "FMATERIALID,FMATERIALID.FNAME,FMaterialName,FMATERIALID.FNumber,FPRICEUNITID.FName," +
                "FPriceQty,FEntryTaxRate,FTAXAMOUNTFOR_D,FPrice,FTaxPrice,FNoTaxAmountFor_D,FALLAMOUNTFOR_D," +
                "FRECEIVEAMOUNT,FComment,FOPENSTATUS_D,FORDERNUMBER,F_ora_Text,FORDERENTRYID,FORDERENTRYSEQ,FOPENSTATUS,FOPENSTATUS_D,FROWNOSRC,FALLAMOUNT_D,F_ora_BGDH ,F_ora_BGFPH ";
        String filterString = "FPAYORGID in (100121,100122) and FACCOUNTSYSTEM = ''";
        if (updateTime != null)
            filterString += StringUtils.startsWith(updateTime.toLowerCase().trim(), "and") ? updateTime : " and " + updateTime;
        return this.getDataJson(formId, fieldKeys, filterString);
    }

    /**
     * 获取应付款
     *
     * @param updateTime
     * @return
     */
    public List<JSONObject> getAccountPayable(String updateTime) {

        String formId = "AP_Payable";
        String fieldKeys = "FID,FBillNo,FDATE,FSUPPLIERID,FSUPPLIERID.FNAME,FCURRENCYID.FNAME,FENDDATE_H,FPayConditon,FPayConditon.FNAME," +
                "FPURCHASERID,FPURCHASERID.FNAME,FALLAMOUNTFOR,FCreateDate,FCreatorId.FNAME,FModifierId.FNAME,FModifyDate," +
                "F_ora_Remarks,FOPENSTATUS,FCancelStatus," +
                "" +
                "FEntityDetail_FEntryID,FSRCROWID,FSourceBillNo,FORDERNUMBER,FOrderEntryID" +
                ",FMATERIALID,FMATERIALID.FName,FMATERIALID.FNumber,FBASICUNITID.FName" +
                ",FPriceQty,FEntryTaxRate," +
                "FTAXAMOUNTFOR_D,FPrice,FTaxPrice,FNoTaxAmountFor_D,FALLAMOUNTFOR_D,FComment,FPAYMENTAMOUNT,FOPENSTATUS_D,FALLAMOUNT_D";
        String filterString = "FSETTLEORGID = 100121";
        if (updateTime != null)
            filterString += StringUtils.startsWith(updateTime.toLowerCase().trim(), "and") ? updateTime : " and " + updateTime;
        return this.getDataJson(formId, fieldKeys, filterString);
    }

    /**
     * 增量同步物料
     *
     * @param updateTime
     * @return
     */

    public List<JSONObject> getItemIncrement(String updateTime) {
        return getItemByWhere((StringUtils.isBlank(updateTime) ? null : "FCreateDate > '" + updateTime + "'"));
    }

    /**
     * 根据更新时间来更新物料
     * @param conditions
     * @return
     */
    public List<JSONObject> getItemIncrementNew(String conditions) {
        return getItemByWhereNew((StringUtils.isBlank(conditions) ? null : conditions));
    }

    public List<JSONObject> getItemByIds(Collection<String> itemIds) {
        return getItemByWhere(CollectionUtils.isEmpty(itemIds) ? null : "FMATERIALID in (" + String.join(",", itemIds) + ")");
    }

    private List<JSONObject> getItemByWhere(String where) {
        String formId = "BD_MATERIAL";
        String fieldKeys = "FMATERIALID,FName,FNumber,FDocumentStatus" +
                ",FMaterialGroup,FMaterialGroup.FName,FSpecification,FMaterialSRC" +
                ",FBaseUnitId.FName,FAuxUnitID.FName,FIncreaseQty,FPlanSafeStockQty" +
                ",FWorkShopId,FWorkShopId.FName,FStockId,FStockId.FName" +
                ",FStockPlaceId,FPurPrice_CMK,FERPCLSID,FNETWEIGHT,FGROSSWEIGHT,FCHECKINCOMING" +
                "" +
                ",FCurrencyId.FName,F_ora_Combo,FExpPeriod,FExpUnit" +
                ",F_ORA_piece,F_ORA_figure,FIsSale,FIsPurchase,FIsInventory" +
                "" +
                ",FOrderQty" +
                "" +
                ",FForbidStatus" +
                ",FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ",FApproverId.FName,FApproveDate" +
                ",FUseOrgId, FUseOrgId.FNumber, FUseOrgId.FName";
        String filterString = "(FUseOrgId = 100121 or FUseOrgId = '100124' or FUseOrgId = '878319') " +
                (StringUtils.isNotBlank(where) ? " and " + where : "") +
                "";
        return getDataJson(formId, fieldKeys, filterString);
    }


    private List<JSONObject> getItemByWhereNew(String where) {
        String formId = "BD_MATERIAL";
        String fieldKeys = "FMATERIALID,FName,FNumber,FDocumentStatus" +
                ",FMaterialGroup,FMaterialGroup.FName,FSpecification,FMaterialSRC" +
                ",FBaseUnitId.FName,FAuxUnitID.FName,FIncreaseQty,FPlanSafeStockQty" +
                ",FWorkShopId,FWorkShopId.FName,FStockId,FStockId.FName" +
                ",FStockPlaceId,FPurPrice_CMK,FERPCLSID,FNETWEIGHT,FGROSSWEIGHT,FCHECKINCOMING" +
                "" +
                ",FCurrencyId.FName,F_ora_Combo,FExpPeriod,FExpUnit" +
                ",F_ORA_piece,F_ORA_figure,FIsSale,FIsPurchase,FIsInventory" +
                "" +
                ",FOrderQty" +
                "" +
                ",FForbidStatus" +
                ",FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ",FApproverId.FName,FApproveDate" +
                ",FUseOrgId, FUseOrgId.FNumber, FUseOrgId.FName,FAuxPropertyId.FName,FAuxPropertyId,FIsEnable1,FForbidStatus,FIsBatchManage";
        String filterString = "(FUseOrgId = 100121 or FUseOrgId = '100124' or FUseOrgId = '878319') " +
                (StringUtils.isNotBlank(where) ? " and " + where : "") +
                "";
        return getDataJson(formId, fieldKeys, filterString);
    }

//    public List<JSONObject> getStock() {
//        String formId = "BD_STOCK"; //
//        String fieldKeys = "" +
//                "FStockFlexDetail_FDetailID,FFlexEntryName" +
//                "";
//        String filterString = "";
//        return this.getDataJson(formId, fieldKeys, filterString);
//    }
//
//    public Map<Long, String> getStockMap() {
//        List<JSONObject> result = getStock();
//        if (result == null)
//            return new HashMap<>();
//        return result.stream().collect(Collectors.toMap(
//                a -> a.getLong("FStockFlexDetail_FDetailID"), a -> a.getString("FFlexEntryName")
//        ));
//    }

    /**
     * 金蝶收款单
     *
     * @return
     */
    public List<JSONObject> getAccountReceived(String conditions) {
        String formId = "AR_RECEIVEBILL";
        String fieldKeys = "FID,FRECEIVEBILLENTRY_FEntryID,FBillNo" +
                ",FBillTypeID.FName" +
                ",FSRCBILLID" +
                ",FCURRENCYID.FName,FREALRECAMOUNTFOR_D,FREALRECAMOUNT_D,FSETTLETYPEID.FName" +
                ",FRECBANKID,FACCOUNTID,FACCOUNTID.FName,FACCOUNTID.FNumber,FDATE" +
                ",FPAYUNIT,FPAYUNIT.FName,FPAYUNITTYPE" +
                ",FWRITTENOFFAMOUNTFOR_D" +
                ",FREMARK,FCOMMENT,FCancelStatus" +
                ",FPAYORGID,FPAYORGID.FName" +

                ",FCreatorId,FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ",FApproverId.FName,FApproveDate" +
                "";
        String filterString = "FPAYORGID in (100121,100122) " +
                (StringUtils.isNotBlank(conditions) ? " and " + conditions : "");
        return getDataJson(formId, fieldKeys, filterString);
    }

    public List<JSONObject> getUnit() {
        String formId = "BD_UNIT"; //
        String fieldKeys = "FNumber,FName,FUnitGroupId,FUnitGroupId.FName,FUnitGroupId.FNumber" +
                "";
        String filterString = "";
        return this.getDataJson(formId, fieldKeys, filterString);
    }

    /**
     * 银行存款期初余额
     *
     * @return 数据
     */
    public List<JSONObject> getAccountAmount(String condition) {
        String formId = "CN_BANKACNTBALINIT";
        String fieldKeys = "FID,FBillNo,FMAINBOOKID.FName,FEXCHANGETYPE.FName,FDate" +
                "" +
                ",FEntity_FEntryID,FCurrencyId.FName" +
                ",FBankAcntId,FBankAcntId.FNumber,FBankAcntName,FBankId.FName" +
                ",FEntAmount,FBankAmount" +
//                ",FDC" +
                ",FISBALANCE" +
//                ",FINNERACCOUNTID,FINNERACCOUNTNAME" +
//                ",FNOREACHENTAMOUNT,FNOREACHBANKAMOUNT" +
//                ",FYEARDEBITAMOUNT,FYEARCREDITAMOUNT" +
//                ",FYEARDEBITAMOUNTLOC,FYEARCREDITAMOUNTLOC" +
//                ",FNOREACHENTAMOUNTLOC,FNOREACHBANKAMOUNTLOC" +
                ",FEntAmountLoc,FBankAmountLoc" +
                ",FEXCHANGERATE" +
                "" +
                ",FDocumentStatus,FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ",FApproverId.FName,FApproveDate" +
                "";
        String filterString = "FPAYORGID = 100121" +
                (StringUtils.isNotBlank(condition) ? " and " + condition : "");
        return this.getDataJson(formId, fieldKeys, filterString);
    }


    /**
     * 获取所有科目
     *
     * @return /
     */
    public List<JSONObject> getSubject(String condition) {
        String formId = "BD_Account"; //
        String fieldKeys = "FACCTID,FNumber,FName,FFULLNAME,FDC,FGROUPID.FNumber,FGROUPID.FName" +
                ",FGROUPID,FCurrencyName,FAllCurrency,FITEMDETAILID.FNumber,FITEMDETAILID.FNAME" +
                ",FParentID,FParentID.FName,FAuditorID,FAuditorID.FName,FAuditorID.FPHONE" +
                ",FAuditDate,FDocumentStatus" +
                ",FACCTTBLID.FNumber,FACCTTBLID.FName" +
                ",FForbidStatus" +
                ",FUseOrgId,FCreateOrgId" +
                ",FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ""; //
        String filterString = "FUseOrgId in (1,100121)" + (condition != null ? condition : "");
        return this.getDataJson(formId, fieldKeys, filterString);
    }

    public List<JSONObject> getSubjectType(String condition) {

        String formId = "BD_AccountType"; //
        String fieldKeys = "FName,FNumber,FDescription,FLevel" +
                ",FAcctGroupId,FAcctGroupId.FName,FAcctGroupId.FNumber" +
                ",FAcctTableId,FAcctTableId.FName,FAcctTableId.FNumber" +
//                        ",FCreateOrgId,FUseOrgId" +
                ",FParentId,FLevel" +
                ",FDocumentStatus,FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ",FAuditorId.FName,FAuditDate" +
                "";
        String filterString = "FUseOrgId in (1,100121)";
        return this.getDataJson(formId, fieldKeys, filterString);
    }


    /**
     * 根据时间获取凭证
     *
     * @return /
     */
    public List<JSONObject> getVoucher(String condition, Collection<String> subject) {
        List<JSONObject> result = new ArrayList<>();
        String formId = "GL_VOUCHER";
        String fieldKeys = "FVOUCHERID,FBillNo" +
                ",FDATE,FYEAR,FPERIOD,FVOUCHERGROUPID.FNAME" +
                ",FVOUCHERGROUPNO" +
                ",FSourceBillKey.FName,FBUSDATE" +

                ",FEntity_FEntryID" +
                ",FPOSTERID.FNAME,FCASHIERID.FNAME" +
                ",FDocumentStatus,FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ",FCHECKERID.FName,FAuditDate" +
                ",FInvalid" +
                "" +
                ",FEXCHANGERATETYPE.FName,FEXCHANGERATE,FDC" +
//                        ",FSETTLENO,FBUSNO,FQty,FPrice,FUnitId,FAcctUnitQty,FBaseUnitQty" +
                ",FEXPLANATION,FACCOUNTID,FACCOUNTID.FNUMBER,FACCOUNTNAME,FACCTFULLNAME" +
                ",FCURRENCYID.FNAME" +
                ",FAMOUNTFOR,FDEBIT,FCREDIT,FAmount" +
                ",FCASHFLOWITEM,FEXPORTENTRYID";
//                111715 = 001
        for (String s : subject) {
            String filterString = "FAccountBookID in (111715,111716,111717) " + (condition != null ? " and " + condition : "") +
                    " and FACCOUNTID.FNUMBER like '" + s + "%')";
            result.addAll(this.getDataJson(formId, fieldKeys, filterString));
        }
        return result;


    }

    public List<JSONObject> getReportByType() {
        List<JSONObject> result = new ArrayList<>();
        String formId = "KDS_Report";
        String fieldKeys = "FRptName,FRptType" +
                "";
        String filterString = "";
        return this.getDataJson(formId, fieldKeys, filterString);

    }

    public List<JSONObject> getSTKInventory(String conditions) {
        List<JSONObject> result = new ArrayList<>();
        String formId = "STK_Inventory";
        String fieldKeys = "FID,FMaterialId,FMaterialName,FStockOrgId,FStockOrgId.FNAME,F_ORA_BASEPROPERTY1," +
                "FStockLocId, FMATERIALID.FNUMBER,FStockId,FStockName,FStockId,FStockId.FNAME,FKFPeriod,FKFPeriodUnit," +
                "FProduceDate,FExpiryDate,FQTY,FBaseQty,FBaseAVBQty,FSecQty,FAVBQty,FSecAVBQty,FProjectNo,FAVBQty,FSecAVBQty,FStockUnitId.FNAME,FSecUnitId.FNAME,FLot.FNumber," +
                "FAUXPROPID,"+
                "FAUXPROPID.FF100001.fdatavalue" +
                ",FAUXPROPID.FF100002.fdatavalue" +
                ",FAUXPROPID.FF100003" +
                ",FAUXPROPID.FF100004" +
                ",FAUXPROPID.FF100005.fdatavalue" +
                ",FAUXPROPID.FF100006.fdatavalue" +
                ",FAUXPROPID.FF100007" +
                ",FAUXPROPID.FF100008" +
                ",FAUXPROPID.FF100009" +
                ",FAUXPROPID.FF100011.FDATAVALUE" +
                ",FAUXPROPID.FF100013" +
                ",FAUXPROPID.FF100015.fdatavalue" +
                ",FAUXPROPID.FF100016.fdatavalue" +
                ",FAUXPROPID.FF100017.fdatavalue" +
                ",FAUXPROPID.FF100018.fdatavalue" +
                ",FAUXPROPID.FF100018.FNUMBer" +
                ",FAUXPROPID.FF100019.fdatavalue" +
                ",FAUXPROPID.FF100020.fdatavalue" +
                ",FAUXPROPID.FF100021" +
                ",FAUXPROPID.FF100022" +
                ",FAUXPROPID.FF100023" +
                ",FUpdateTime";
        String filterString = "FBaseQty > 0 ";
//                (StringUtils.isNotBlank(conditions) ? " and " + conditions : "");
        return getDataJson(formId, fieldKeys, filterString);
    }


    public List<JSONObject> getSTKInventory(String filterString, int start, int size) {
        List<JSONObject> result = new ArrayList<>();
        String formId = "STK_Inventory";
        String fieldKeys = "FID,FMaterialId,FMaterialName,FStockOrgId,FStockOrgId.FNAME,F_ORA_BASEPROPERTY1," +
                "FStockLocId, FMATERIALID.FNUMBER,FStockId,FStockName,FStockId,FStockId.FNAME,FKFPeriod,FKFPeriodUnit," +
                "FProduceDate,FExpiryDate,FQTY,FBaseQty,FBaseAVBQty,FSecQty,FAVBQty,FSecAVBQty,FProjectNo,FAVBQty,FSecAVBQty,FStockUnitId.FNAME,FSecUnitId.FNAME,FLot.FNumber," +
                "FAUXPROPID," +
                "FAUXPROPID.FF100001.fdatavalue" +
                ",FAUXPROPID.FF100002.fdatavalue" +
                ",FAUXPROPID.FF100003" +
                ",FAUXPROPID.FF100004" +
                ",FAUXPROPID.FF100005.fdatavalue" +
                ",FAUXPROPID.FF100006.fdatavalue" +
                ",FAUXPROPID.FF100007" +
                ",FAUXPROPID.FF100008" +
                ",FAUXPROPID.FF100009" +
                ",FAUXPROPID.FF100011.FDATAVALUE" +
                ",FAUXPROPID.FF100013" +
                ",FAUXPROPID.FF100015.fdatavalue" +
                ",FAUXPROPID.FF100016.fdatavalue" +
                ",FAUXPROPID.FF100017.fdatavalue" +
                ",FAUXPROPID.FF100018.fdatavalue" +
                ",FAUXPROPID.FF100018.FNUMBer" +
                ",FAUXPROPID.FF100019.fdatavalue" +
                ",FAUXPROPID.FF100020.fdatavalue" +
                ",FAUXPROPID.FF100021" +
                ",FAUXPROPID.FF100022" +
                ",FAUXPROPID.FF100023" +
                ",FUpdateTime";
        String condition = "FBaseQty > 0 and FStockName like '%钢板库%'";
        filterString = (StringUtils.isNotBlank(filterString) ? condition + " and " + filterString : condition + "");
        return getDataJson(formId, fieldKeys, filterString, start, size);
    }

    /**
     * 金蝶采购入库单
     * @param conditions
     * @return
     */
    public List<JSONObject> getPurmInbound(String conditions) {
        List<JSONObject> result = new ArrayList<>();
        String formId = "STK_InStock";
        String fieldKeys = "FID,FBillNo,FSupplierId,FSupplierId.FNAME,FDate,FSettleCurrId,FSettleCurrId.FNAME,FSettleTypeId,FSettleTypeId.FNAME," +
                "FBillAllAmount,FPurchaserId,FPurchaserId.FNAME,FCreatorId.FNAME,FCreateDate,FModifierId.FNAME,FModifyDate," +
                "FApproverId,FApproverId.FNAME,FApproveDate,FAPSTATUS,FDocumentStatus,FCancelStatus," +
                "" +
                "FSRCBILLTYPEID,FInStockEntry_Link_FSBillId,FInStockEntry_Link_FSId,FPOORDERENTRYID," +
                "" +
                "FInStockEntry_FEntryID,FSRCRowId,FMaterialId,FMaterialName,FMATERIALID.FNUMBER,FPriceUnitID,FPriceUnitID.FNAME," +
                "FExtAuxUnitId.FNAME,FPrice,FTaxPrice,FAllAmount,FAllAmount_LC,FAllAmount,FAllAmount_LC,FDiscountRate," +
                "FEntryTaxAmount,FEntryTaxRate,FTaxAmount_LC,FStockId,FStockId.FNAME,FStockLocId,FExtAuxUnitQty,F_ora_Qty1,FLot," +
                "FRealQty,FMustQty,FENTRYCOSTAMOUNT,FEntryCostAmount,FCostAmount_LC," +
                "FAUXPROPID," +
                "FAUXPROPID.FF100001.fdatavalue" +
                ",FAUXPROPID.FF100002.fdatavalue" +
                ",FAUXPROPID.FF100003" +
                ",FAUXPROPID.FF100004" +
                ",FAUXPROPID.FF100005.fdatavalue" +
                ",FAUXPROPID.FF100006.fdatavalue" +
                ",FAUXPROPID.FF100007" +
                ",FAUXPROPID.FF100008" +
                ",FAUXPROPID.FF100009" +
                ",FAUXPROPID.FF100011.FDATAVALUE" +
                ",FAUXPROPID.FF100013" +
                ",FAUXPROPID.FF100015.fdatavalue" +
                ",FAUXPROPID.FF100016.fdatavalue" +
                ",FAUXPROPID.FF100017.fdatavalue" +
                ",FAUXPROPID.FF100018.fdatavalue" +
                ",FAUXPROPID.FF100018.FNUMBER" +
                ",FAUXPROPID.FF100019.fdatavalue" +
                ",FAUXPROPID.FF100020.fdatavalue" +
                ",FAUXPROPID.FF100021" +
                ",FAUXPROPID.FF100022" +
                ",FAUXPROPID.FF100023"
                ;
        String filterString = "FStockOrgId = 100121 " +
                (StringUtils.isNotBlank(conditions) ? " and " + conditions : "") +
                "";
        return getDataJson(formId, fieldKeys, filterString);
    }

    public List<JSONObject> getPaidApply(String condition) {
        String formId = "CN_PAYAPPLY";
        String fieldKeys = "FID,FBillNo,FBILLTYPEID.FName,FDATE" +
                ",FAPPLYAMOUNTFOR_H,FAPPLYAMOUNT,FPAYAMOUNTFOR_H,FSETTLECUR.FName" +
                ",F_ora_Decimal,F_ora_Remarks,F_ora_Date" +
                ",FPURCHASERID,FPURCHASERID.FName" +
                ",FCONTACTUNITTYPE,FCONTACTUNIT,FCONTACTUNIT.FName" +
                "" +
                ",FPAYAPPLYENTRY_FEntryID,FCOSTID.FName,FSOURCETYPE" +
                ",FPAYAPPLYENTRY_Link_FSBillId,FPAYAPPLYENTRY_Link_FSId,FSRCBILLNO" +
                ",FSETTLETYPEID.FName,FPAYPURPOSEID,FENDDATE,FEXPECTPAYDATE" +
                ",FPAYAMOUNTFOR,FAPPLYAMOUNTFOR,FRELATEPAYAMOUNT" +
                ",FEACHBANKACCOUNT,FEACHCCOUNTNAME,FEACHBANKNAME,FENDDATE" +
                "" +
                ",FDocumentStatus,FCreatorId,FCreatorId.FName,FCreateDate,FCANCELSTATUS" +
                ",FModifierId.FName,FModifyDate" +
                ",FAPPROVERID.FName,FAPPROVEDATE" +
                "";
        String filterString = "FAPPLYORGID = 100121" + (condition != null ? " and " + condition : "") +
                "";
        return this.getDataJson(formId, fieldKeys, filterString);

    }

    public List<JSONObject> getPurchaseRequisition(String condition) {
        String formId = "PUR_Requisition";
        String fieldKeys = "FID,FBillNo,FBillTypeID.FName,FApplicationDate" +
                ",FApplicationDeptId,FApplicationDeptId.FName,FApplicantId,FApplicantId.FName" +
                ",FCurrencyId.FName,FTotalAmount" +
                ",FDocumentStatus,FApproverId.FName,FApproveDate" +
                ",FCloserId.FName,FCloseStatus,FCloseDate,FMRPTerminateStatus,FTerminaterId.FName,FTerminateDate" +
                ",FApplicationOrgId,FApplicationOrgId.FName,FNote" +
                ",FCreatorId.FName,FCreateDate" +
                ",FModifierId.FName,FModifyDate" +
                ",FCancelStatus" +
                "" +
                ",FEntity_FEntryID,FMaterialId,FMaterialId.FName,FMaterialId.FNumber" +
                ",FUnitId.FName,F_ora_UnitID.FName,FReqQty,F_ora_Qty" +
                ",FTAXPRICE,FReqAmount" +
                ",FSuggestSupplierId,FSuggestSupplierId.FName,FSuggestPurDate" +
                ",FStockQty,FApproveQty,FOrderQty,FRemainQty" +
                "" +
                ",FEntryNote,FMRPCloseStatus,F_ora_Base.F_ORA_TEXT" +
                "" +
                ",FAUXPROPID.FF100001.fdatavalue" +
                ",FAUXPROPID.FF100002.fdatavalue" +
                ",FAUXPROPID.FF100003" +
                ",FAUXPROPID.FF100004" +
                ",FAUXPROPID.FF100005.fdatavalue" +
                ",FAUXPROPID.FF100006.fdatavalue" +
                ",FAUXPROPID.FF100007" +
                ",FAUXPROPID.FF100008" +
                ",FAUXPROPID.FF100009" +
                ",FAUXPROPID.FF100011" +
                ",FAUXPROPID.FF100013" +
                ",FAUXPROPID.FF100015.fdatavalue" +
                ",FAUXPROPID.FF100016.fdatavalue" +
                ",FAUXPROPID.FF100017.fdatavalue" +
                ",FAUXPROPID.FF100018.fdatavalue" +
                ",FAUXPROPID.FF100019.fdatavalue" +
                ",FAUXPROPID.FF100020.fdatavalue" +
                ",FAUXPROPID.FF100021" +
                ",FAUXPROPID.FF100022" +
                ",FAUXPROPID.FF100023" +
                ",F_ora_Text3" +
                "";
        String filterString = "FApplicationOrgId = 100121" +
                (condition != null ? " and " + condition : "");
        return this.getDataJson(formId, fieldKeys, filterString);
    }

    public List<JSONObject> getStore() {
        String formId = "BD_STOCK";
        String fieldKeys = "FStockId,FNumber,FName,FSTOCKPROPERTY,FDescription,FGroup,FCreatorId," +
                "FCreatorId.FNAME,FCreateDate,FModifierId,FModifierId.FNAME,FModifyDate,FPRINCIPAL,FPRINCIPAL.FNAME";
        String filterString = "FUseOrgId = 100121";
        return this.getDataJson(formId, fieldKeys, filterString);
    }

    public List<JSONObject> getStoreLocation() {
        String formId = "BD_STOCK";
        String fieldKeys = "FStockId," +
                "FStockFlexItem_FEntryID,FFlexId,FFlexName,FFlexDescription,FIsMustInput,FFlexNumber," +
                "FStockFlexDetail_FDetailID,FFlexEntryId,FNumber,FFlexEntryName,FFlexEntryDescription,FFlexEntryForbid";
        String filterString = "";
        return this.getDataJson(formId, fieldKeys, filterString);
    }

    public List<JSONObject> getStoreLocationFid() {
        String formId = "BD_FLEXSITEMDETAILV";
        String fieldKeys = "FID,FNAME";
        String filterString = "";
        return this.getDataJson(formId, fieldKeys, filterString);
    }


    public List<JSONObject> getExchangeRate(String condition) {
        String formId = "BD_Rate";
        String fieldKeys = "FRateID,FForbidStatus,FName,FDescription,FExchangeRate,FReverseExRate,FBegDate,FEndDate,FCyForID.FName" +
                "" +
                ",FAuditorID,FAuditorID.FName,FAuditorID.FPHONE" +
                "" +
                ",FDocumentStatus,FCreatorId,FCreatorId.FName,FCreateDate,FForbidStatus" +
                ",FModifierId.FName,FModifyDate";
        String filterString = (condition != null ? condition : "") +
                " and FCyToID.FName = '人民币'";
        return this.getDataJson(formId, fieldKeys, filterString);

    }

    /**
     * 板材申领单
     */
    public List<JSONObject> getPlateReq(String condition) {
        String formId = "PAAP_req";
        String fieldKeys = "FID, FBillNo, FDocumentStatus, FApproverId, FApproverId.FName, FApproveDate, FModifierId.FName, FCreateDate, FCreatorId.FName, " +
                "FModifyDate, FDescription, FDate, FStockerId, FStockerId.FName, FWorkShopId, FWorkShopId.FName, F_ora_Text4, " +
                // 以下是应发板材明细表
                "FEntity_FEntryID, FACTUALQTY, FMaterialId, FMaterialName, FSpecification, FUnitID, FUnitID.FName " +
                ",FAUXPROPID.FF100001.fdatavalue" +
                ",FAUXPROPID.FF100002.fdatavalue" +
                ",FAUXPROPID.FF100003" +
                ",FAUXPROPID.FF100004" +
                ",FAUXPROPID.FF100005.fdatavalue" +
                ",FAUXPROPID.FF100006.fdatavalue" +
                ",FAUXPROPID.FF100007" +
                ",FAUXPROPID.FF100008" +
                ",FAUXPROPID.FF100009" +
                ",FAUXPROPID.FF100011" +
                ",FAUXPROPID.FF100013" +
                ",FAUXPROPID.FF100015.fdatavalue" +
                ",FAUXPROPID.FF100016.fdatavalue" +
                ",FAUXPROPID.FF100017.fdatavalue" +
                ",FAUXPROPID.FF100018.fdatavalue" +
                ",FAUXPROPID.FF100019.fdatavalue" +
                ",FAUXPROPID.FF100020.fdatavalue" +
                ",FAUXPROPID.FF100021" +
                ",FAUXPROPID.FF100022" +
                ",FAUXPROPID.FF100023" +
                ",FStockId,FStockId.FName" +
                // 以下是应发板材明细表
                ",F_PAAP_Entity1_FEntryID,FPartNumber,FMapNumber,FPartNumber1,FworkingProcedure,F_caizhi_paiban ,F_guige_paiban ,F_LENGTH_paiban ,F_WIDTH_paiban ,F_Project_paiban ,F_qty_paiban ,F_weight_paiban ,F_sumweight_paiban    "+
                // 退料板材明细
                ",F_PAAP_Entity_FEntryID,FmaterielNumberTUI,FMaterialNameTUI,F_LENGTH_TUI,F_WIDTH_TUI,F_BatchNumber_TUI,F_UnitID_TUI,F_Qty_TUI,F_auxUnitID_TUI,F_auxQty_TUI";
        String filterString = "FCancelStatus = 'A' " +
        (condition != null ? " and " + condition : "");
        return this.getDataJson(formId, fieldKeys, filterString);
    }

    /**
     * 金蝶生产入库单
     * @param conditions
     * @return
     */
    public List<JSONObject> getProduceInbound(String conditions) {
        String formId = "PRD_INSTOCK";
        String fieldKeys = "FID,FBillNo,FStockOrgId,FStockOrgId.FNAME,FOwnerId0,FOwnerId0.FNAME, " +
                " FBillType.FNAME  ,FSTOCKERID ," +
                " FDate ,FPrdOrgId,FPrdOrgId.FNAME ," +
                " FDocumentStatus ,FDescription,FCancelStatus ,FCreatorId.FNAME,FCreateDate,FModifierId.FNAME,FModifyDate, " +
                // 明细
                " FEntity_FEntryID,FMaterialId,FMATERIALID.FNUMBER,FMaterialName,FSpecification,FProductType ,FInStockType  ," +
                " FUnitID.FNAME,FMustQty ,FRealQty ," +
                " FStockId,FStockId.FNAME ,FLot.FNumber";
        String filterString = "FStockOrgId = 100121 " +
                (StringUtils.isNotBlank(conditions) ? " and " + conditions : "") +
                "";
        return getDataJson(formId, fieldKeys, filterString);
    }

    /**
     * 金蝶简单生产入库单
     * @param conditions
     * @return
     */
    public List<JSONObject> getSimpleInbound(String conditions) {
        String formId = "SP_InStock";
        String fieldKeys = "FID,FBillNo, FBillType.FNAME,FDate,FDocumentStatus ," +
                "FStockOrgId,FStockOrgId.FNAME ,FPrdOrgId ,FPrdOrgId.FNAME  ,FBillStatus,FOwnerId0,FOwnerId0.FNAME ,FDescription ,FCancelStatus ,FCreatorId.FNAME,FCreateDate,FModifierId.FNAME,FModifyDate," +
                // 明细
                "FEntity_FEntryID,FMaterialId,FMaterialId.FNUMBER,FMaterialName,FSpecification,FUnitID,FUnitID.FNAME,FMustQty ,FRealQty ," +
                "FStockId,FStockId.FNAME ,FLot.FNumber,FWorkShopId1.FNAME,FOwnerId,FOwnerId.FNAME,Fxiangmujingli.FNAME ";

        String filterString = "FStockOrgId = 100121 " +
                (StringUtils.isNotBlank(conditions) ? " and " + conditions : "") +
                "";
        return getDataJson(formId, fieldKeys, filterString);
    }


    /**
     * 金蝶开工令
     * @param conditions
     * @return
     */
    public List<JSONObject> getProject(String conditions) {
        String formId = "k441ff31b955046bab1f7c2b819d60501";
        String fieldKeys = "F_ora_Text2,F_ora_Text1,F_ora_Text3,FCreateDate,FBILLSTATUS,F_BEQ_BASE2" +
                ",FCreatorId.FName,FModifierId.FName,FModifyDate,FID,F_BEQ_BASE2.FSTAFFNUMBER,F_BEQ_Integer,F_BEQ_Base1,F_BEQ_BaseProperty";

        String filterString =  StringUtils.isNotBlank(conditions) ? " " + conditions : "";
        return getDataJson(formId, fieldKeys, filterString);
    }

    /**
     * 去金蝶验证开工令是否被删除
     * @param unCheckFids 待验证的开工令fid
     * @return 已被金蝶删除的开工令fid
     */
    public List<Long> projectDeleteConfirm(List<Long> unCheckFids) {
        List<Long> toDeleteFids = new ArrayList<>();
        if(CollUtil.isEmpty(unCheckFids)){
            return toDeleteFids;
        }
        String formId = "k441ff31b955046bab1f7c2b819d60501";
        String fieldKeys = "FID";

        String filterString =  " FID in ("
        +unCheckFids.stream()
                .map(Object::toString) // 将每个元素转换为字符串类型
                .collect(Collectors.joining(","))+")";
        List<JSONObject> data = getDataJson(formId, fieldKeys, filterString);
        Set<Long> existFids = data.stream().map(item->item.getLong("FID")).collect(Collectors.toSet());

        for (Long unCheckFid : unCheckFids) {
            if(!existFids.contains(unCheckFid)){
                toDeleteFids.add(unCheckFid);
            }

        }
        return toDeleteFids;
    }


    /**
     * 销售普通发票
     */
    public List<JSONObject> getSALESOC(String condition) {
        String formId = "IV_SALESOC";
        String fieldKeys = "FID, FBillNo, FDocumentStatus, FApproverId, FApproverId.FName, FApproveDate, FModifierId.FName, FCreateDate,FCancelStatus, FCreatorId.FName, " +
                "FModifyDate, FDate,FCUSTOMERID,FCUSTOMERID.FNAME,FCURRENCYID.FNAME,FSaleOrgId,FSaleOrgId.FName" +
                ",FBILLINGWAY,FAFTERTOTALTAXFOR,FIVNUMBER,FINVOICEDATE,F_ora_Text3,F_ora_Text2,FSETTLEORGID,FSETTLEORGID.fname" +
                // 以下明细表
                ",FSALESICENTRY_FEntryID, FPriceUnitId.FName, FAUXTAXPRICE, FPriceQty, FAUXPRICE, FMATERIALID.FNUMBER, FMATERIALNAME, FAMOUNTFOR, FALLAMOUNTFOR" +
                ",FNOTAXAMOUNT,FAllAmount,FLot.FName,F_ora_Text";
        String filterString = "FCancelStatus = 'A' " +
                (condition != null ? " and " + condition : "");
        return this.getDataJson(formId, fieldKeys, filterString);
    }

    /**
     * 销售专用发票
     */
    public List<JSONObject> getSpecialSALESOC(String condition) {
        String formId = "IV_SALESIC";
        String fieldKeys = "FID, FBillNo, FDocumentStatus, FApproverId, FApproverId.FName, FApproveDate, FModifierId.FName, FCreateDate,FCancelStatus, FCreatorId.FName, " +
                "FModifyDate, FDate,FCUSTOMERID,FCUSTOMERID.FNAME,FCURRENCYID.FNAME,FSaleOrgId,FSaleOrgId.FName" +
                ",FBILLINGWAY,FAFTERTOTALTAXFOR,FIVNUMBER,FINVOICEDATE,F_ora_BGFPH,F_ora_BGDH,FSETTLEORGID,FSETTLEORGID.fname" +
                // 以下明细表
                ",FSALESICENTRY_FEntryID, FPriceUnitId.FName, FAUXTAXPRICE, FPriceQty, FAUXPRICE, FMATERIALID.FNUMBER, FMATERIALNAME, FAMOUNTFOR, FALLAMOUNTFOR" +
                ",FNOTAXAMOUNT,FAllAmount,FLot.FName,F_ora_Text";
        String filterString = "FCancelStatus = 'A' " +
                (condition != null ? " and " + condition : "");
        return this.getDataJson(formId, fieldKeys, filterString);
    }

    /**
     * 收料通知单
     *
     * @param conditions
     * @return
     */
    public List<JSONObject> getPurmTake(String conditions) {
        List<JSONObject> result = new ArrayList<>();
        String formId = "PUR_ReceiveBill";
        String fieldKeys = "FID,FBillNo,FDate,FSupplierId,FSupplierId.FNAME,FDate,FSettleCurrId,FSettleCurrId.FNAME,FStockOrgId,FStockOrgId.Fname, " +
                "FBillAllAmount,FPurchaserId,FPurchaserId.FNAME,FCreatorId.FNAME,FCreateDate,FModifierId.FNAME,FModifyDate," +
                "FApproverId,FApproverId.FNAME,FApproveDate,FDocumentStatus,FCancelStatus," +
                "" +
                " FPOORDERENTRYID,FContactNo,FDetailEntity_Link_FSBillId,FDetailEntity_Link_FSId," +
                "" +
                " FMaterialId,FMaterialName, FMATERIALID.FNUMBER,FPriceUnitID,FPriceUnitID.FNAME," +
                "FExtAuxUnitId.FNAME,FPrice,FTaxPrice,FAllAmount,FAllAmount_LC,FAllAmount,FAllAmount_LC,FDiscountRate," +
                "FEntryTaxAmount,FEntryTaxRate,FTaxAmount_LC,FStockId,FStockId.FNAME,FStockLocId,FExtAuxUnitQty,FLot,FActReceiveQty,FDetailEntity_FEntryID" +
                ",FAUXPROPID.FF100001.fdatavalue" +
                ",FAUXPROPID.FF100002.fdatavalue" +
                ",FAUXPROPID.FF100003" +
                ",FAUXPROPID.FF100004" +
                ",FAUXPROPID.FF100005.fdatavalue" +
                ",FAUXPROPID.FF100006.fdatavalue" +
                ",FAUXPROPID.FF100007" +
                ",FAUXPROPID.FF100008" +
                ",FAUXPROPID.FF100009" +
                ",FAUXPROPID.FF100011" +
                ",FAUXPROPID.FF100013" +
                ",FAUXPROPID.FF100015.fdatavalue" +
                ",FAUXPROPID.FF100016.fdatavalue" +
                ",FAUXPROPID.FF100017.fdatavalue" +
                ",FAUXPROPID.FF100018.fdatavalue" +
                ",FAUXPROPID.FF100019.fdatavalue" +
                ",FAUXPROPID.FF100020.fdatavalue" +
                ",FAUXPROPID.FF100021" +
                ",FAUXPROPID.FF100022" +
                ",FAUXPROPID.FF100023";
        String filterString = "(FStockOrgId = 100121)" +
                (StringUtils.isNotBlank(conditions) ? " and " + conditions : "") +
                "";
//        String filterString = "(FStockOrgId = 100124 or  FStockOrgId = 105846)" ;
        return getDataJson(formId, fieldKeys, filterString);
    }
    public List<JSONObject> getBDEmpinfo(String condtion) {
        String formId = "BD_Empinfo";
        String fieldKeys = "FID,FDocumentStatus,FName,FNumber,FMobile,FPostDept.FNumber,FStaffNumber";
        String filterString = "";
        return this.getDataJson(formId, fieldKeys, filterString);
    }


    /**
     * 检验单
     *
     * @param filterString
     * @return
     */
    public List<JSONObject> getPurmPurchaseTake(String filterString) {
        //金蝶的检验单表
        String formId = "QM_InspectBill";
        //FSrcBillNo0  收料单编号
        //FSrcEntryId0（源单分录内码）对应purm_purchase_take_detail（收料通知单详情）的detail_id（唯一id）
        //FEntryID（实体主键）为金蝶检验单表明细id
        String fieldKeys = "FID,FSrcInterId0,FSrcEntryId0,FEntity_FEntryID";
        return getDataJson(formId, fieldKeys, filterString);
    }

    /**
     * 费用项目
     * @param condition
     * @return
     */
    public List<JSONObject> getExpenseProject(String condition) {
        String formId = "BD_Expense";
        String fieldKeys = "FEXPID,FName,FNumber,FGroup.FName,FDescription,FRATE.FName,FCreatorId.FName,FCreateDate, FModifierId.FName,FModifyDate";
        String filterString = (condition != null ? condition : "") +
                "";
        return this.getDataJson(formId, fieldKeys, filterString);

    }

}
